package com.example.jack.community.view.home.homeAdapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.example.jack.community.R;

import java.util.ArrayList;

/**
 * Created by vveng on 2017/11/7 19: 05
 *
 * @mail: vvengStruggle@163.com
 */

public class HomeItemRcAdapter extends RecyclerView.Adapter<HomeItemRcAdapter.HomeItemRcViewHolder> {

  private Context mContext;
   // private int[] ImageIds;
    private ArrayList<String> urls = new ArrayList<>();
    public HomeItemRcAdapter(Context context, ArrayList<String> urllist) {
        mContext = context;
        this.urls = urllist;
    }
        public interface OnRcItemListener{
            void onRcItemClick(View view, ArrayList<String> urls, int posistion);
        }
    private OnRcItemListener mOnRcItemListener;

    public void setOnRcItemListener(OnRcItemListener onRcItemListener) {
        mOnRcItemListener = onRcItemListener;
    }

    @Override
    public HomeItemRcViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (urls.size() == 1){
            View view = LayoutInflater.from(mContext).inflate(R.layout.home_item_rc_item_one, parent, false);
            final HomeItemRcViewHolder holder = new HomeItemRcViewHolder(view);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = holder.getAdapterPosition();
                    mOnRcItemListener.onRcItemClick(holder.itemView,urls,position);
                }
            });
            return holder;
        }else{
            View view = LayoutInflater.from(mContext).inflate(R.layout.home_item_rc_item, parent, false);
            final HomeItemRcViewHolder holder = new HomeItemRcViewHolder(view);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = holder.getAdapterPosition();
                    mOnRcItemListener.onRcItemClick(holder.itemView,urls,position);
                }
            });
            return holder;
        }
    }

    @Override
    public void onBindViewHolder(HomeItemRcViewHolder holder, int position) {
        if (urls.size()==1){
            Glide.with(mContext).load(urls.get(position)).thumbnail(0.1f).into(holder.mImageView2);
        }else{
            Glide.with(mContext).load(urls.get(position)).thumbnail(0.1f).into(holder.mImageView1);
        }

    }

    @Override
    public int getItemCount() {
        return urls.size();
    }

    public class HomeItemRcViewHolder extends RecyclerView.ViewHolder {
        private ImageView mImageView1;
        private ImageView mImageView2;
        public HomeItemRcViewHolder(View itemView) {
            super(itemView);

            mImageView1 = (ImageView) itemView.findViewById(R.id.home_item_rc_image);
            mImageView2 = (ImageView) itemView.findViewById(R.id.home_item_rc_item_one_image);
        }
    }

}
